package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.bean.choice.NovelModel;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.ui.view.choice.base.ChoiceTopicNovelBaseView;

import java.util.List;

public class ChoiceTopicColumnView extends ChoiceTopicNovelBaseView {
    private Context context;
    private TextView tvTitle;
    private TextView tvLeftTitle,tvLeftOneTitle,tvLeftOneDesc,tvLeftTwoDesc,tvLeftThreeDesc,tvLeftFourDesc,tvLeftMore;
    private TextView tvRightTitle,tvRightOneTitle,tvRightOneDesc,tvRightTwoDesc,tvRightThreeDesc,tvRightFourDesc,tvRightMore;
    private ImageView ivLeftOnePic,ivRightOnePic;
    private RelativeLayout layRightOne,layLeftOne;
    private String title;
    private LinearLayout layRight,layLeft;


    public ChoiceTopicColumnView(Context context) {
        super(context);
    }

    public ChoiceTopicColumnView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicColumnView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicColumnView(Context context, ChoiceTopicBean bean) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_column_view, null);
        this.context = context;
        title = bean.getTitle();
        initView(view);
        initTop(view,bean);
        llMore.setVisibility(GONE);

        setData(bean.getComplexList());
        addView(view);
    }

    private void initView(View view) {
        layRightOne = view.findViewById(R.id.layRightOne);
        layLeftOne = view.findViewById(R.id.layLeftOne);
        tvTitle = view.findViewById(R.id.tvTitle);
        tvLeftTitle = view.findViewById(R.id.tvLeftTitle);
        tvLeftOneTitle = view.findViewById(R.id.tvLeftOneTitle);
        tvLeftOneDesc = view.findViewById(R.id.tvLeftOneDesc);
        tvLeftTwoDesc = view.findViewById(R.id.tvLeftTwoDesc);
        tvLeftThreeDesc = view.findViewById(R.id.tvLeftThreeDesc);
        tvLeftFourDesc = view.findViewById(R.id.tvLeftFourDesc);
        tvLeftMore = view.findViewById(R.id.tvLeftMore);
        tvRightTitle = view.findViewById(R.id.tvRightTitle);
        tvRightOneTitle = view.findViewById(R.id.tvRightOneTitle);
        tvRightOneDesc = view.findViewById(R.id.tvRightOneDesc);
        tvRightTwoDesc = view.findViewById(R.id.tvRightTwoDesc);
        tvRightThreeDesc = view.findViewById(R.id.tvRightThreeDesc);
        tvRightFourDesc = view.findViewById(R.id.tvRightFourDesc);
        tvRightMore = view.findViewById(R.id.tvRightMore);
        ivLeftOnePic = view.findViewById(R.id.ivLeftOnePic);
        ivRightOnePic = view.findViewById(R.id.ivRightOnePic);
        layRight = view.findViewById(R.id.layRight);
        layLeft = view.findViewById(R.id.layLeft);
        int margin = ScreenUtil.dip2px(getContext(), 32);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((ScreenUtil.getScreenWidth(getContext())-margin)/2,LayoutParams.WRAP_CONTENT);
        params.leftMargin = ScreenUtil.dip2px(getContext(), 8);
        params.topMargin = ScreenUtil.dip2px(getContext(), 12);
        layLeft.setLayoutParams(params);
        layRight.setLayoutParams(params);
    }

    private void setDesc(TextView tvDesc,NovelModel model) {
        if (tvDesc == null) {
            return;
        }
        if (model == null) {
            return;
        }
        if (!TextUtils.isEmpty(model.getBrief())) {
            tvDesc.setText(model.getBrief());
        } else if (!TextUtils.isEmpty(model.getDescription())) {
            tvDesc.setText(model.getDescription());
        }
    }


    public void setData(List<ChoiceTopicBean> list) {
        if (list.size() > 0) {
            final ChoiceTopicBean leftBean = list.get(0);
            if (leftBean != null) {
                List<NovelModel> leftList = leftBean.getNovelList();
                if (leftList.size() == 0) {
                    layLeft.setVisibility(GONE);
                }else{
                    layLeft.setVisibility(VISIBLE);
                }
                tvLeftTitle.setText(leftBean.getTitle());
                if (leftList.size() > 0) {
                    final NovelModel leftMode1 = leftList.get(0);
                    ChoiceUtils.displayImage(context,leftMode1.getCover(),ivLeftOnePic);
                    tvLeftOneTitle.setText(leftMode1.getTitle());
                    setDesc(tvLeftOneDesc,leftMode1);
                    layLeftOne.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(leftMode1.getId());
                        }
                    });
                }
                if (leftList.size() > 1) {
                    final NovelModel leftMode2 = leftList.get(1);
                    setDesc(tvLeftTwoDesc,leftMode2);
                    tvLeftTwoDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(leftMode2.getId());
                        }
                    });
                }
                if (leftList.size() > 2) {
                    final NovelModel leftMode3 = leftList.get(2);
                    setDesc(tvLeftThreeDesc,leftMode3);
                    tvLeftThreeDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(leftMode3.getId());
                        }
                    });
                }
                if (leftList.size() > 3) {
                    final NovelModel leftMode4 = leftList.get(3);
                    setDesc(tvLeftFourDesc,leftMode4);
                    tvLeftFourDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(leftMode4.getId());
                        }
                    });
                }
                tvLeftMore.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_LEFT_MORE,eventData);
                        ChoiceUtils.jumpToMoreNovel(getContext(),title,leftBean.getMoreUrl());
                    }
                });
            }
        }
        if (list.size() > 1) {
            final ChoiceTopicBean rightBean = list.get(1);
            if (rightBean != null) {
                List<NovelModel> rightList = rightBean.getNovelList();
                if (rightList.size() == 0) {
                    layRight.setVisibility(GONE);
                }else{
                    layRight.setVisibility(VISIBLE);
                }
                tvRightTitle.setText(rightBean.getTitle());
                if (rightList.size() > 0) {
                    final NovelModel rightMode1 = rightList.get(0);
                    ChoiceUtils.displayImage(context,rightMode1.getCover(),ivRightOnePic);
                    tvRightOneTitle.setText(rightMode1.getTitle());
                    setDesc(tvRightOneDesc,rightMode1);
                    layRightOne.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(rightMode1.getId());
                        }
                    });
                }
                if (rightList.size() > 1) {
                    final NovelModel rightModel2 = rightList.get(1);
                    setDesc(tvRightTwoDesc,rightModel2);
                    tvRightTwoDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(rightModel2.getId());
                        }
                    });
                }
                if (rightList.size() > 2) {
                    final NovelModel rightModel3 = rightList.get(2);
                    setDesc(tvRightThreeDesc,rightModel3);
                    tvRightThreeDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            jumpToNovelDetail(rightModel3.getId());
                        }
                    });
                }
                if (rightList.size() > 3) {
                    final NovelModel rightModel4 = rightList.get(3);
                    setDesc(tvRightFourDesc,rightModel4);
                    tvRightFourDesc.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            jumpToNovelDetail(rightModel4.getId());
                        }
                    });
                }
                tvRightMore.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_RIGHT_MORE,eventData);
                        ChoiceUtils.jumpToMoreNovel(getContext(),title,rightBean.getMoreUrl());
                    }
                });
            }
        }
    }

    private void jumpToNovelDetail(int novelId) {
        ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
        ChoiceUtils.jumpToNovelDetail(context,novelId);
    }

    @Override
    public void updateList(List<NovelModel> list) {

    }
}
